package com.example.feign;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import feign.Feign;
import feign.Target;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.okhttp.OkHttpClient;
import io.netty.handler.codec.http.HttpResponseEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClientsConfiguration;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * 动态url
 * http://cloud.spring.io/spring-cloud-static/Camden.SR6/#_creating_feign_clients_manually
 * https://stackoverflow.com/questions/43733569/how-can-i-change-the-feign-url-during-the-runtime
 */
@RestController
@RequestMapping("httpDyna2")
@Import(FeignClientsConfiguration.class)
public class HttpDynaController2 {


    private HttpDynaConsumeService httpDynaConsumeService;


    @RequestMapping("/getGoodsList/{sysId}/{parentId}")
    public String getGoodsList(@PathVariable("sysId") String sysId,@PathVariable("parentId") String parentId) throws URISyntaxException {
        Map<String,Object> map=new HashMap<>();
        map.put("sysId",sysId);
        map.put("parentId",parentId);
        StringBuffer url=new StringBuffer("http://hqb.t.nxin.com/openservice/getGoodsList.shtml");
        url.append("?");
        for(String key:map.keySet()){
            url.append(key+"="+map.get(key));
            url.append("&");
        }
        String reqUrl=url.toString().substring(0,url.length()-1);
        httpDynaConsumeService = Feign.builder()
                .client(new OkHttpClient())
                .target(HttpDynaConsumeService.class,reqUrl);
        return httpDynaConsumeService.getGoodsList();
    }


}
